import json

# 读取 total.json 文件
with open('total.json', 'r', encoding='utf-8') as file:
    data = json.load(file)

# 初始化空字典
admission_data = {}

# 遍历原始数据并转换格式
for entry in data:
    year = entry["year"]
    province = entry["province"]
    total_value = int(entry["totalValue"])

    # 如果年份不在字典中，初始化一个空字典
    if year not in admission_data:
        admission_data[year] = {}

    # 如果省份不在该年份的字典中，初始化为当前的 totalValue
    if province not in admission_data[year]:
        admission_data[year][province] = total_value
    else:
        # 如果省份已存在，累加 totalValue
        admission_data[year][province] += total_value

# 将结果写入到 map.json 文件
with open('map.json', 'w', encoding='utf-8') as outfile:
    json.dump(admission_data, outfile, ensure_ascii=False, indent=4)

print("数据已写入 map.json 文件")
